package de.hm.edu.kinderfinder.pojos;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.location.Location;
import android.util.Log;

/**
 * Diese Klasse stellt die objektorientierte Repräsentation einer Zone dar.
 * 
 * @author Christopher Samuel Pitter, Lukas Jarosch
 * 
 */
public class ZoneObject {

	private float latitude;

	public float getLatitude() {
		return latitude;
	}

	public void setLatitude(float latitude) {
		this.latitude = latitude;
	}

	public float getLongitude() {
		return longitude;
	}

	public void setLongitude(float longitude) {
		this.longitude = longitude;
	}

	public long getTimeStart() {
		return timeStart;
	}

	public void setTimeStart(long timeStart) {
		this.timeStart = timeStart;
	}

	public long getTimeEnd() {
		return timeEnd;
	}


	public void setTimeEnd(long timeEnd) {
		this.timeEnd = timeEnd;
	}

	public String getZonename() {
		return zonename;
	}

	public void setZonename(String zonename) {
		this.zonename = zonename;
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	private float longitude;
	private long timeStart;
	private long timeEnd;
	private String zonename;
	private int radius;
	private boolean monday;
	private boolean tuesday;
	private boolean wednesday;
	private boolean thursday;
	private boolean friday;
	private boolean saturday;
	private boolean sunday;

	public boolean isMonday() {
		return monday;
	}

	public void setMonday(boolean monday) {
		this.monday = monday;
	}

	public boolean isTuesday() {
		return tuesday;
	}

	public void setTuesday(boolean tuesday) {
		this.tuesday = tuesday;
	}

	public boolean isWednesday() {
		return wednesday;
	}

	public void setWednesday(boolean wednesday) {
		this.wednesday = wednesday;
	}

	public boolean isThursday() {
		return thursday;
	}

	public void setThursday(boolean thursday) {
		this.thursday = thursday;
	}

	public boolean isFriday() {
		return friday;
	}

	public void setFriday(boolean friday) {
		this.friday = friday;
	}

	public boolean isSaturday() {
		return saturday;
	}

	public void setSaturday(boolean saturday) {
		this.saturday = saturday;
	}

	public boolean isSunday() {
		return sunday;
	}

	public void setSunday(boolean sunday) {
		this.sunday = sunday;
	}

	/**
	 * Die Zeitlogic, die überprüft ob eine Zone zum Zeitpunkt des aufrufens aktiv
	 * geschalten ist.
	 * @return ein boolean, true wenn die zone gerade aktiv ist
	 */
	public boolean isAlarmIfLeftZoneNow() {
		Date date = new Date();
		// Calendar cal = new GregorianCalendar(date.getYear(),date.getMonth(),
		// date.getDay(),date.getHours(),date.getMinutes(),date.getSeconds());
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		int currentDayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // Sun=1, Mon=2,
																// ..Sam=7
		switch (currentDayOfWeek) {
		case 1:
			if (isSunday())
				return checkTimeFrame();
			break;
		case 2:
			if (isMonday())
				return checkTimeFrame();
			break;
		case 3:
			if (isTuesday())
				return checkTimeFrame();
			break;
		case 4:
			if (isWednesday())
				return checkTimeFrame();
			break;
		case 5:
			if (isThursday())
				return checkTimeFrame();
			break;
		case 6:
			if (isFriday())
				return checkTimeFrame();
			break;
		case 7:
			if (isSaturday())
				return checkTimeFrame();
			break;
		default:
			break;
		}
		return false;
	}

	/**
	 * Helfermethode für  isAlarmIfLeftZoneNow().
	 * @return ein boolean
	 */
	private boolean checkTimeFrame() {
		Date fromDate = new Date(getTimeStart());
		Date toDate = new Date(getTimeEnd());
		Date now = new Date();

		fromDate.setYear(now.getYear());
		fromDate.setMonth(now.getMonth());
		fromDate.setDate(now.getDate());
		toDate.setYear(now.getYear());
		toDate.setMonth(now.getMonth());
		toDate.setDate(now.getDate());

		return (fromDate.before(now) && toDate.after(now));
	}

	/**
	 * Testet ob ein Punkt innerhab dieser Zone liegt oder nicht.
	 * @param locationObject
	 * @return ein boolean
	 */
	public boolean isInZone(LocationObject locationObject) {
		float[] result = new float[3];
		Location.distanceBetween(latitude, longitude, locationObject.getLatt(),
				locationObject.getAtt(), result);
		Log.i("pk", "Distance Check " + result[0]);
		return result[0] < radius;
	}

}
